﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using AMS.Inventory.DataAccess;
using AMS.Inventory.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI;

namespace AMSInventoryWeb.BaseData
{
    public partial class BaseDataInfoList : System.Web.UI.Page
    {
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private ObjXmlReader m_XmlReader;

        public string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataInfoSetting.xml"; }
        }

        private string ClassName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("ClassName", this); }
        }

        private string TypeName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("TypeName", this); }
        }

        private string QueryCondition
        {
            get
            {
                
                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }

        private XmlNode GetSettingNode(string objTypeName)
        {
            return m_XmlReader.GetXmlNode("Setting/" + objTypeName);
        }

        private string GetSettingNodeAttrVal(string objTypeName,string attrName)
        {
            XmlNode node = GetSettingNode(objTypeName);
            if (node != null)
            {
                return m_XmlReader.GetNodeAttributeValue(node, attrName,true);
            }
            return "";
        }


        private string GetObjTypeName()
        {
            string strVal = "";
            if (ClassName.Length > 0)
                strVal = ClassName;
            if (TypeName.Length > 0)
                strVal = strVal + "_" + TypeName;
            return strVal;
        }

        private void InitExCtl()
        {
            PageBuilder bulider = new PageBuilder(this, "");
            string strObjTypeName = GetObjTypeName();
            XmlNode node = GetSettingNode(strObjTypeName);
            bulider.SetPgListAttribute(pgList, node, false, false, false);
            m_CreateCtlHlp.SetDdlCondition(pgList, ddlCondition);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (m_XmlReader == null)
                m_XmlReader = new ObjXmlReader(SettingXmlPath);
            NavigateCtl1.ItemInfo = new NameValueCollection();
            InitExCtl();
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {


            pgList.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
            if (!IsPostBack)
            {
                PagePermissionHelper.CheckIsLoginToDefault(this);
                
            }
        }


        private object BindPgList()
        {
            string objTypeName = GetObjTypeName();
            DataSet ds;
            var acc = new BaseDataAccess();
            var sqlHelper = new SqlHelper();
            string sql = GetSettingNodeAttrVal(objTypeName, "sql");
            sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            sql += " " + QueryCondition;
            acc.BeginTransaction();
            try
            {
                ds = sqlHelper.ExecuteDataSet(acc.Session, sql);
            }
            finally
            {
                acc.CloseSession();
            }
            return ds;
        }


        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
            pgList.BindGrid();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
            pgList.BindGrid();
        }

    }
}
